
function delatt(o)
{
	if(confirm("确定移除?"))
	{
		$(o.parentNode.parentNode).remove();
	}
}
function delproduct(id)
{
	if(confirm("确定移除?"))
	{
		$.post("/oversea/ajaxbutton", {"op": "delproduct","id":id},
		  function(data){
				
			document.location.href = "/oversea/product";
			return;
		   },"json");
	}
}
function delwarehouse(id)
{
	if(confirm("确定移除?"))
	{
		$.post("/oversea/ajaxbutton", {"op": "delwarehouse","id":id},
		  function(data){
				
			document.location.href = "/oversea/warehouse";
			return;
		   },"json");
	}
}
function delstatuslist(id,planid)
{
	if(confirm("确定移除?"))
	{
		$.post("/oversea/ajaxbutton", {"op": "delstatuslist","id":id},
		  function(data){
			document.location.href = "/oversea/showtask/"+planid;
			return;
		   },"json");
	}
}
function getpaixu(id,o)
{
		$.post("/oversea/ajaxbutton", {"op": "getpaixu","id":id,"paixu":o.value},
		  function(data){
			document.location.href = "/oversea/showtask/"+planid;
			return;
		   },"json");

}


function deltask(id)
{
	if(confirm("确定撤销吗?"))
	{
		$.post("/oversea/ajaxbutton", {"op": "deltask","id":id},
		  function(data){
			document.location.href = "/oversea/transportlist";
			return;
		   },"json");
	}
}

function getsku(id)
{
	var sku = $("#sku").val();
	$.post("/oversea/ajaxbutton", {"op": "getsku","sku":sku},
		  function(data){
					if(data==200)
					{
					
					}else{
							$("#skuerr").html('商品编号(SKU)['+sku+']已经存在！');
							$("#sku").val('');
					return false;
					}
			return;
		   },"json");
}

function saveproduct()
{
	if($("#sku").val()=="")
	{
		$("#skuerr").html('商品编号(SKU)不能为空！');
		return false;
	}
	if($("#name").val()=="")
	{
		$("#nameerr").html('不能为空！');
		return false;
	}
	if($("#costprice").val()=="")
	{
		$("#costpriceerr").html('商品成本价不能为空！');
		return false;
	}
	$("#theFrom").submit();
}
function purchasingNext()
{
	var seawarehouse = $("#seawarehouse").val();

	var shopname = $("#shopname").val();
	var sku = $("#sku").val();
	if(seawarehouse=="")
	{
		alert("请选择发往仓库!");
		
		return;
	}
	
	/*
	var endhouse = $("#endhouse").val();
	if(endhouse.indexOf('Amazon') >=0)
	{
		if($("#upfiles").val()=="")
		{
			alert("亚马逊FBA必须上传标签!");
			$("#upfiles").focus();
			return;
		}

	}*/
	

	if(seawarehouse.indexOf('Amazon') >=0)
	{
		if(shopname=="")
		{
			alert("目的地为Amazon时，必须选择店铺！");
				return;
		}
	
	}

	if($("#transportmode").val() =="")
	{
	
		alert("运输方式必填！");
		return;
	
	}
	
	if(sku=="")
	{
		alert("请填写sku!");
		$("#sku").focus();
		return;
	}
	$.post("/oversea/ajaxbutton", {"op": "savepurchasingNext","seawarehouse":seawarehouse,"shopname":shopname,"sku":sku},
	  function(datats){
			if(datats ==202)
			{
				alert("没有找到SKU!");
				return;
			}else{
				if(datats.nosku)
				{
					alert("存在未列入海外仓列表的SKU 如下：\r\n"+datats.nosku+"");
				}
				var data = datats.listdata;
				

				var tmp='<br><table cellpadding="0" cellspacing="0" border="0" class="stdtable">';
				tmp+=' <colgroup><col class="con1" /><col class="con0" /><col class="con1" /><col class="con0" /><col class="con1" /><col class="con0" /><col class="con1" /></colgroup>';
				tmp+='<thead><th class="head0">移除</th><th class="head0">商品编号</th><th class="head1">商品名称</th><th class="head0">成本价</th><th class="head0">数量</th><th class="head0">备注</th></tr> </thead>';
				for(var i=0;i<data.length;i++)
				{
					if(data[i].isst==1 || data[i].ishw==1)
					{
						var values ="  readonly value='0'";
					}else{
						var values ="";
					}


					tmp+=" <tr>";
					tmp +="<td><img src=/fuman/images/del.png class=p_del  style=\"cursor:pointer\" title=\"不需要这个SKU\" onclick=\"delatt(this)\"></td><td>"+data[i].sku+"</td><td>"+data[i].name+"</td><td>"+data[i].costprice+"</td><td><input type=\"text\" name=\"skusum[]\"  id=\"skusum\" class=\"longinput\" "+values+" style='width:100px;' /></td><td>"+data[i].stmemo+""+data[i].hwmemo+"<input type=\"hidden\" name=\"skuid[]\" value="+data[i].id+"  /><input type=\"hidden\" name=\"isst[]\" value="+data[i].isst+"  /><input type=\"hidden\" name=\"ishw[]\" value="+data[i].ishw+"  /><input type=\"hidden\" name=\"goodsno[]\" value="+data[i].sku+"  /></td>" ;
					tmp+=" </tr>";
				}
				tmp+="</table>";
				//$("#sku").attr("disabled","disabled");
				$("#prespan").html(''); 
				$("#prespan").html('<button type="button" class="submit radius2" id="btn" onClick="purchasingNext()">追加SKU</button>&nbsp;&nbsp;&nbsp;<button type="button" class="submit radius2" id="btn" onClick="savepurchasing()">确定保存</button>'); 
    		      $("#showskulist").html(tmp);    
			}
	   },"json");
	
	
}
function savepurchasing()
{
	var st=0;
	$("input[id='skusum']").each(function () {
			if ($(this).val() == "") {
				alert("数量必须全部填写，不能留空！");
				$(this).focus();
				st = 1;
				return;
			}
		})
	if(st==0){
	$("#theFrom").submit();
		}
}
function delpurlist(id)
{
	if(confirm("确定移除?"))
	{
		$.post("/oversea/ajaxbutton", {"op": "delpurlist","id":id},
		 function(data){
				
			document.location.href = "/oversea/purchasinglist";
				return;
			

	   },"json");
	}
}

function getpurchprolist()
{
	var endhouse = $("#endhouse").val();
	var shopname = $("#shopname").val();
	if(endhouse.indexOf('Amazon') >=0)
	{
		if(shopname=="")
		{
			alert("目的地为Amazon时，必须选择店铺");
			return;
		}

	}

	return;



	$.post("/oversea/ajaxbutton", {"op": "getpurchprolist","endhouse":endhouse,"shopname":shopname},
	  function(data){
			if(data ==202)
			{
				alert("没有找到SKU!");
				return;
			}else{

				
				var newti = "";
				var is =0;
				var tmp='<br><table cellpadding="0" cellspacing="0" border="0" class="stdtable">';
				tmp+=' <colgroup><col class="con1" /><col class="con0" /><col class="con1" /><col class="con0" /><col class="con1" /><col class="con0" /><col class="con1" /></colgroup>';
				tmp+='<thead><th class="head0"><input name="allchk" id="allchk" type="checkbox" onClick="allSelect(\'competenceID[]\')"></th><th class="head0">明细</th><th class="head0">总计需发数量</th><th class="head0">商品编号</th><th class="head1">商品名称</th><th class="head0">成本价</th></tr> </thead>';
				for(var i=0;i<data.length;i++)
				{
					is = 1;
						var goodslist = data[i].goodslist;
						newti+=' <table cellpadding="0" cellspacing="0" border="0" class="stdtable">';
						for(var k=0;k<goodslist.length;k++)
						{
							newti+=" <tr >";
							newti+= "<td>"+goodslist[k].createUser+"</td><td>预订："+goodslist[k].skunum+"</td><td><input type=\"text\" name=\"realsend["+data[i].oversearproduct_id+"][]\" value="+goodslist[k].skunum+" id=\"realsend\" class=\"longinput\" style='width:30px;' /><input type=\"hidden\" name=\"overseapurchasing["+data[i].oversearproduct_id+"][]\" value="+goodslist[k].id+"  /></td>";
							newti+=" </tr>";
						}
						newti+=" </table>";

						if(data[i].st==1)
							{
								var bg="bgcolor=\"#FFD2D2\" title='带商标商品'";
							}else{
								var bg= "";
							}
					tmp+=" <tr "+bg+">";
					tmp +="<td><input type=\"checkbox\" name=\"competenceID[]\"  value="+data[i].oversearproduct_id+" /> </td><td>"+newti+"</td><td>"+data[i].totalnum+"</td><td>"+data[i].goodsno+"</td><td>"+data[i].name+"</td><td>"+data[i].costprice+"</td>" ;
					tmp+=" </tr>";
					newti = "";
				}
				tmp+="</table>";
				if(is==1){

					//alert(tmp);
					//$("#endhouse").attr("disabled","disabled");
					$("#prespan").show(); 
					$("#prespan").html('<button type="button" class="submit radius2" id="btn" onClick="savepacking()">生成发货计划</button>'); 

					$("#showskulist").html(tmp);
				}
				if(data.length<=0)
				{
					$("#prespan").hide(); 
					$("#showskulist").html('');
				}
				 
    		   
			}
	   },"json");
}

function savepacking()
{
	if($("#transportmode").val()=="")
	{
		alert("请选择运输方式!");
		$("#transportmode").focus();
		return;
	}
	if($("#carrier").val()=="")
	{
		alert("请选择计划承运人!");
		$("#carrier").focus();
		return;
	}
	var st = 0;
	 $("#stdtable").find("input[type='text']").each(function () {
			if ($(this).val() == "") {
				st = 1;
			}
		})
	if(st ==  1)
	{
		alert("FNSKU必须全部填写!");
		return;
	}

	$("#theFrom").submit();
}


/********
**	d 对象
**  goodsno t_overseapurchasing ID号
**  cod 可放入箱子数
**  sendnums 实际需要发货数
**  container 可选择的箱号
*/
function altshone(d,goodsno,cod){
	if(cod==9999)
	{
		return;
	}
	$("#showerr"+goodsno).html('');
	var inputvalue = d.value;	//输入值
	if(cod){
		var oldnum =  document.getElementById(cod+"oldnum"+goodsno);	//可放入货柜数
	}else{
		var oldnum =  document.getElementById("oldnum"+goodsno);	//可放入货柜数

	}
	
	if(inputvalue==""){
		return;
	}
	if(parseInt(inputvalue) > parseInt(cod)){
		$("#showerr"+goodsno).html('装箱数量超过计划发货数量');
		//d.value = cod;
		return;
	}
	
}
function addRow(id) {

	var skuname = $("#skuname").val();
	var innerHTML="";
	 $.post("/oversea/ajaxbutton", {"op": "onegoods","skuname":skuname,"planid":id},
		 function(data){
		 if(data==201)
		 {
			alert("本次发货计划中未有找到该SKU!");
			return;
		 }else{
			 data = eval(data);
		 for(var i=0;i<data.length;i++){
			 innerHTML+= '<tr><td>'+data[i].sku+'<input type="hidden" name="TDid[]" value='+data[i].pid+'></td>';
			  innerHTML+= ' <td>'+data[i].sku+'</td>';
			 innerHTML+= ' <td>'+data[i].createUser+'</td>';
			 innerHTML+= ' <td>'+data[i].skunum+'</td>';
			 innerHTML+= ' <td>'+data[i].sendnums+'</td>';
			 innerHTML+="<td><input type='hidden' name='TDcostprice[]'  value="+data[i].costprice+">";
			 innerHTML+= '<input type="text" name="TDnowbuynum[]" class="width100" onBlur="altshone(this,'+data[i].pid+',9999)" ></td></tr>';
		 }
		 $("#addrowlist").append(innerHTML);

	}
	
		},"json");

	
	
	
   }

function showaddRow()
{
	$("#showrow").show();
}
function changstatus(t,id,type){

	if(confirm("你确定执行此操作?"))
	{
		$.post("/oversea/ajaxbutton", {"op": "changstatus","t":t,"id":id,"type":type},
		 function(data){
				if(type==1)
				{
					if(t>=4)
					{
						window.location.href="/oversea/deliveryplanlist";
					}else{
						window.location.href = "/oversea/purchasinglist";
					}
				}else{
					window.location.href = "/oversea/transportlist";
				}
				return;
	   },"json");
	  
	}
}



function getTchangstatus(t,id){

	if(confirm("你确定执行此操作?"))
	{
		$.post("/oversea/ajaxbutton", {"op": "getTchangstatus","t":t,"id":id},
		 function(data){
				window.location.href = "/oversea/purchasinglist";
				return;
	   },"json");
	  
	}
}

function askchangstatus(t,id,type)
{
	$("#ist").val(t);
	$("#isid").val(id);
	$("#istype").val(type);
	$('#backtip').modal('show');

}
function actchangstatus()
{
	var t = $("#ist").val();
	var id = $("#isid").val();
	var type = $("#istype").val();
	var back_message = $("#back_message").val();
	$.post("/oversea/ajaxbutton", {"op": "changstatus","t":t,"id":id,"type":type,"back_message":back_message},
		 function(data){
				if(type==1)
				{
					window.location.href = "/oversea/purchasinglist";
				}else{
					window.location.href = "/oversea/transportlist";
				}
				return;
	   },"json");
}

function savecompare()
{
	if($("#box").val()=="")
	{
		$("#boxerr").html('&nbsp;<font color=red>请填写箱号！</font>');
		return false;
	}
	
	if($("#weight").val()=="")
	{
		$("#weighterr").html('&nbsp;<font color=red>请填写重量！</font>');
		return false;
	}

	
	$("#theFrom").submit();
}
function getbox()
{
	$("#boxerr").html('');
	var boxid = $("#groupid").html()+$("#box").val();
	if(boxid)
	{
		$.post("/oversea/ajaxbutton", {"op": "boxid","boxid":boxid},
		 function(data){
				
			if(data==201)
			{
				$("#boxerr").html('&nbsp;<font color=red>箱号已经存在！</font>');
				$("#box").val('')
				return false;
			}
			

	   },"json");
	}
	
}
function deliveryplanlist(id)
{
	if(confirm("确定移除?"))
	{
		$.post("/oversea/ajaxbutton", {"op": "deldelitt","id":id},
		 function(data){
				
			document.location.href = "/oversea/deliveryplanlist";
				return;
			

	   },"json");
	}
}

var totalnum = 1;
function showinput()
{
	var inputn = document.getElementById("input1");
	var str=inputn.innerHTML;
	var num=1;
	for(var i=1;i<=num;i++){
		totalnum=totalnum+i;
		str += "<tr id=las"+totalnum+">";
        str+="<td><input type=\"text\" class=\"width100\"   name=\"freight[]\"  />&nbsp;&nbsp;运费说明：<input type=\"text\" class=\"width100\"   name=\"projectdateremake[]\"  /><a href=javascript:void(0) onclick=delfileinput("+totalnum+")><span style=\"padding:5px 0 0\" class=\"c9\">移除</span></a></td>";
        str+="</tr>";
	}
	$("#input1").html(str);
	
}
function delfileinput(id)
{
	if(confirm("确定移除？"))
	{
		$("#las"+id).remove();
	}
}

function changisQuality()
{
	  var str = selectallpro();

	 if (str != false)
	{
		 $.post("/oversea/ajaxbutton", {"op": "changisQuality","uid":str},
			 function(data){
				
				  if(data==200)
				 {
					  window.location.reload();
					return;
				 }
			   },"json");
	 }
}

function chggoodsstatus()
{
    var vote_arr = document.getElementsByName("ppid[]");
	var idarr = new Array();
	var i=0;
	var str="";
	for(var j = 0; j < vote_arr.length; j++)
	{
	   if(vote_arr[j].checked == true)
	   {
	       idarr[i] = vote_arr[j].value;
		   str += vote_arr[j].value+",";
		   i++;
	   }
	   
	}
	if(i<=0){
			alert("未选中数据");
		} else{
			//s=1问题订单
				 $.post("/oversea/ajaxbutton", {"op": "orderproblem","uid":str},
				 function(data){
					 if(data==201)
					 {
						alert("所选择的项始发地不一样");
						return;
					 }
					  if(data==202)
					 {
						alert("所选择的目的地不一样");
						return;
					 }
					  if(data==203)
					 {
						alert("有未核验的箱子，不能发货！请先安排核验");
						return;
					 }
					  if(data==200)
					 {
						window.location='/oversea/transportboxtypedetail?uid='+str;
						return;
					 }
				   		//	
				   },"json");
	}
}
function do_qa(id)
{
	//AWS.ajax_post($('#qaform'));
	$("#id").val(id);
	
	 $.post("/oversea/ajaxbutton", {"op": "getplanstatustask","id":id},
		 function(data){
			if(data !=201)
			{
				$("#projectdate").val(data.projectdate);
				$("#endprojectdate").val(data.endprojectdate);
				$("#projectdateremake").val(data.projectdateremake);
				$("#cargostatus").val(data.cargostatus);
				$("#carrier").val(data.carrier);
				$("#transportmode").val(data.transportmode);
				$('#returndataModal').modal('show');
			}
		   },"json");

	$('#returndataModal').modal('show');
}
function do_merge()
{
	var str = selectallpro();
	if (str != false) {
	  
		$("#demandid_list").val(str);
		$('#returndataModal').modal('show');
	}
	
}
function ediplan(id)
{
	 $.post("/oversea/ajaxbutton", {"op": "getplanstatus","id":id},
				 function(data){
					if(data !=201)
					{
						$("#id").val(data.task_id);
						$("#statusid").val(data.id);
						$("#projectdate").val(data.projectdate);
						$("#endprojectdate").val(data.endprojectdate);
						$("#freight").val(data.freight);

						$("#delivery_fee").val(data.delivery_fee);
						$("#clearance_fee").val(data.clearance_fee);
						$("#tariffs_fee").val(data.tariffs_fee);
						$("#other_fee").val(data.other_fee);

						$("#projectdateremake").val(data.projectdateremake);
						$("#ck").val(data.ck);
						$("#cargostatus").val(data.cargostatus);
						$("#carrier").val(data.carrier);
						$("#transportmode").val(data.transportmode);
						$("#weight").val(data.weight);
						$('#returndataModal').modal('show');
					}
				   },"json");
	
}
function doqa()
{
	AWS.ajax_post($('#qaform'));
	$('#returndataModal').modal('hide');
}
function addgoods(orderID) {
    $("#addgoodsorderID").val(orderID);
    $('#addattributeform').modal('show');

}
/********增加多行商品*******************/
var totalnum = 1;
function showaddgoodsSKU(t) {
    var inputn = document.getElementById(t);
    var str = inputn.innerHTML;
    var num = 1;
    for (var i = 1; i <= num; i++) {
        totalnum = totalnum + i;
        str += "<tr id=las" + totalnum + ">";
        str += "<td>";
        str += "商品编号<input type=\"text\" name=\"ordergoodsno[" + totalnum + "]\" id=\"ordergoodsno\" onBlur=\"getstatusgoods(this)\" class=\"width100\" /> 计划发货数 <input type=\"text\" name=\"ordergoodsnoNum[" + totalnum + "]\" id=\"ordergoodsnoNum\" class=\"width100\" />  &nbsp;<a href=javascript:void(0) onclick=delfileinput(" + totalnum + ") class=\"btn btn_book\"><span>移除</span></a></td>";
        str += "</tr>";
    }
    $("#" + t).html(str);

}
function getstatusgoods(e)
{
	var goods = e.value;
	if(goods)
	{
		//判断是否存在该SKU
		 $.post("/oversea/ajaxbutton", {"op": "getgoodssku", "val": goods},
                function (data) {
                    if (data.code == 201) {
                     e.value = '';
					   alert(data.txt);
                    }

                }, "json");
	}
}
function getpriceexpress()
{
	$("#freight").val('');

	var carrier = $("#carrier").find("option:selected").val(); 
	if(carrier == '顺丰')
	{
		var sfexpress = $("#sfexpress").val();
	}
	if(carrier == 'DHL')
	{
		var sfexpress = $("#dhlexpress").val();
	}
	if(carrier == 'UPS')
	{
		var sfexpress = $("#upsexpress").val();
	}
	if(carrier == 'FedEx')
	{
		var sfexpress = $("#FedExexpress").val();
	}
	if(carrier == '佳成')
	{
		var sfexpress = $("#jiachenexpress").val();
	}
	
	if(sfexpress){
		var weight = $("#weight").val();
		var price = sfexpress * weight;
			price = price.toFixed(2);
		$("#freight").val(price);
	}

}